﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Parking
{
    public partial class CreateAccPage : PhoneApplicationPage
    {
        public CreateAccPage()
        {
            InitializeComponent();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            if (email_textBox.Text.Trim().Length > 0 && passwordBox1.Password.Trim().Length > 0 && passwordBox2.Password.Trim().Length > 0 && email_textBox.Text.Trim().Length > 0)
            {
                if (passwordBox1.Password.Trim() == passwordBox2.Password.Trim())
                {
                    //create the account
                    string URI = "http://parcari.casa-az.ro/main/register_validation/" + HttpUtility.UrlEncode(email_textBox.Text.Trim()) + "/" + HttpUtility.UrlEncode(passwordBox1.Password.Trim()) + "/" + HttpUtility.UrlEncode(passwordBox2.Password.Trim());

                    WebClient wc = new WebClient();
                    wc.DownloadStringAsync(new Uri(URI, UriKind.Absolute));
                    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                }
                else
                {
                    MessageBox.Show("The passwords are not the same!");
                }
            }
            else
            {
                MessageBox.Show("Please fill all the fields!");
            }
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string result = e.Result;
                if (result == "ok")
                {
                    MessageBox.Show("Account created! Please check your email and confirm your account.");
                }
                else
                {
                    MessageBox.Show("There was an error! Please try again later.");
                }
                if (NavigationService.CanGoBack)
                {
                    NavigationService.GoBack();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}